/* jshint globalstrict: true */
/* module: false */
define(['WebPDF', 
	'angularMocks', 
	'app/js/Plugins/Form', 	
	'./FormDataMock',
	'../../../app/js/Plugins/Form/models/FormPage',
    '../../../app/js/Plugins/Form/models/FormWidget',
    '../../../app/js/Plugins/Form/models/FormWidget_Combo',
    '../../../app/js/Plugins/Form/models/FormWidget_List',
    '../../../app/js/Plugins/Form/models/FormWidget_Radio',
    '../../../app/js/Plugins/Form/models/FormWidget_PushBtn',
    '../../../app/js/Plugins/Form/models/FormWidget_Text',
    '../../../app/js/Plugins/Form/models/FormWidget_Check'], 

    function(WebPDF, angularMocks, Form, FormData, FormPage, FormWidget, 
    	FormWidget_Combo, FormWidget_List, FormWidget_Radio,
    	FormWidget_PushBtn, FormWidget_Text, FormWidget_Check) {
		'use strict';

	describe('FormService', function() {
		var $httpBackend;

		beforeEach(module('WebPDF', "FormPlugin"));

		afterEach(function() {	       
	    });

		beforeEach(inject(function(_$httpBackend_) {
			$httpBackend = _$httpBackend_;
			$httpBackend.whenGET('/app/pdfdata/doc-1/form_xmldata_0').respond(FormData[0]);		
			$httpBackend.whenGET('/app/pdfdata/doc-1/form_xmldata_1').respond(FormData[1]);		
			$httpBackend.whenGET('/app/pdfdata/doc-1/form_xmldata_3').respond(FormData[3]);		
		}));


		it('check the existence of PDFDocService', inject(function(FormService) {
			expect(FormService).toBeDefined();
		}));


		it('create formWidget from xml', function (done) {
			inject(function(FormService, _$httpBackend_) {				
				FormService.getFormPage(0).then(
					function(formPage) {						
						var formWidget = formPage.getFormWidgetByName('Combo Box4');
						expect(formWidget instanceof FormWidget_Combo).toBeTruthy();						
						expect(formWidget.options.length).toBe(6);

						formWidget = formPage.getFormWidgetByName('List Box2');
						expect(formWidget instanceof FormWidget_List).toBeTruthy();	
						expect(formWidget.options.length).toBe(13);
						expect(FormService.getItemValue('List Box2')).toBe('c');
										

						formWidget = formPage.getFormWidgetByName('Button1');
						expect(formWidget instanceof FormWidget_PushBtn).toBeTruthy();	
						expect(formWidget.value).toBe('Submit');

						formWidget = formPage.getFormWidgetByName('Check Box3');
						expect(formWidget instanceof FormWidget_Check).toBeTruthy();	
						expect(formWidget.value).toBe('Yes');
						expect(formWidget.bCheck).toBeTruthy();


						formWidget = formPage.getFormWidgetByName('Radio Button5');
						expect(formWidget instanceof FormWidget_Radio).toBeTruthy();	
						expect(formWidget.value).toBe('Yes');
						expect(formWidget.bCheck).toBeFalsy();

						formWidget = formPage.getFormWidgetByName('Text2');
						expect(formWidget instanceof FormWidget_Text).toBeTruthy();	
						expect(formWidget.value).toBe('中国');
						expect(formWidget.bMultiLine).toBeTruthy();

						formWidget = formPage.getFormWidgetByName('Text6');
						expect(formWidget instanceof FormWidget_Text).toBeTruthy();							
						expect(formWidget.bMultiLine).toBeFalsy();
						expect(formWidget.fieldAction['C']).toBeDefined();						
						expect(formWidget.fieldAction['C']).toContain('AFSimple_Calculate("SUM", new Array ("Text1", "Text3"));'); 						
						done();
					},
					function() {
						//expect whatever not to be here
						expect(true).toBe(false);
						done();
					});
				_$httpBackend_.flush();						
			});
		});

		it('some FormWidget_Checks have same name', function(done) {
			inject(function(FormService, _$httpBackend_) {
				
				FormService.getFormPage(1).then(
					function(formPage) {						
						expect(FormService.getItemValue('Check Box3')).toBe('Yes');		
						//如果其中有一个勾选的，原始值是Yes
						expect(FormService.getItemOrigValue('Check Box3')).toBe('Yes');	

						expect(FormService.getItemValue('Check Box4')).toBeNull();		
						//如果其中有一个勾选的，原始值是Yes
						expect(FormService.getItemOrigValue('Check Box4')).toBeNull();	
						done();
					},
					function() {
						//expect whatever not to be here
						expect(true).toBe(false);
						done();
					});
				_$httpBackend_.flush();		
			});
		});

		it('check FormWidget_Radio set value correctly', function(done) {
			inject(function(FormService, _$httpBackend_) {			
				FormService.getFormPage(1).then(
					function(formPage) {						
						expect(FormService.getItemValue('Where hear')).toBeNull();		
						//如果其中有一个勾选的，原始值是Yes
						expect(FormService.getItemOrigValue('Where hear')).toBeNull();	

						expect(FormService.getItemValue('Where hear2')).toBe('Choice3');		
						//如果其中有一个勾选的，原始值是Yes
						expect(FormService.getItemOrigValue('Where hear2')).toBe('Choice3');	
						done();
					},
					function() {
						//expect whatever not to be here
						expect(true).toBe(false);
						done();
					});
				_$httpBackend_.flush();		
			});
		});

		it('check setFormDataByXML', function(done) {
			inject(function(FormService, _$httpBackend_) {							
				FormService.setFormDataByXML(FormData.xmlData);

				FormService.getFormPage(1).then(
					function(formPage) {		
						//setItemOrigValue的不会履盖getItemValue				
						expect(FormService.getItemValue('Check Box6')).toBe('Yes');		
						expect(FormService.getItemValue('Check Box26')).toBe('Yes');	

						FormService.setItemOrigValue('Check Box36', "Yes");	
						expect(FormService.getItemValue('Check Box36')).toBe('Yes');		
						FormService.setItemValue('Check Box36', "No");						
						expect(FormService.getItemValue('Check Box36')).toBe('No');		
						expect(FormService.getItemOrigValue('Check Box36')).toBe('Yes');	

						FormService.setItemOrigValue('Check Box30', "No");	
						expect(FormService.getItemValue('Check Box30')).toBe('Yes');	

						FormService.setItemValue('Undefinded', "No");	
						expect(FormService.getItemOrigValue('Undefinded')).toBeNull();	
						done();
					},
					function() {
						//expect whatever not to be here
						expect(true).toBe(false);
						done();
					});
				_$httpBackend_.flush();		
			});
		});

		it('called repeatedly getFormPage with same parameter should return same data object', function(done){
			inject(function(FormService, $httpBackend) {		
				var firstData = null;
				var secondData = null;
				var thirdData = null;
				FormService.getFormPage(1).then(function(data){
					firstData = data;					
					FormService.getFormPage(1).then(function(data){
						thirdData = data;								
					});
				});

				FormService.getFormPage(1).then(function(data){
					secondData = data;					
				});

				$httpBackend.flush();
				setTimeout(function(){
					expect(firstData === secondData).toBeTruthy();
					expect(firstData === thirdData).toBeTruthy();
					done();
				}, 0);
			});
		});


		it ('check getRegisterActionCode', function(done){
			inject(function(FormService, _$httpBackend_) {				
				FormService.getFormPage(3).then(
					function(formPage) {		
						var formWidget = formPage.getFormWidgetByName('Text6');			
						FormService.setItemValue("Text1", 1);
						FormService.setItemValue("Text3", 2);		
						//AFSimple_Calculate("SUM", new Array ("Text1", "Text3"));				
						expect(FormService.getItemValue('Text6')).toBe(3);


						FormService.setItemValue("Value_0", 2);
						FormService.setItemValue("Value_1", 6);	
						FormService.setItemValue("Value_2", 3);	

						//event.value=(AFMakeNumber(getField("Value_0").value)*AFMakeNumber(getField("Value_1").value))/AFMakeNumber(getField("Value_2").value)
						expect(FormService.getItemValue('Total_1')).toBe(4);

						FormService.setItemValue("Text.1", 2);
						FormService.setItemValue("Text 5", 6);	
						//AFSimple_Calculat("SUM", new Array ("Text.1", "Text 5"));
						expect(FormService.getItemValue('Text 7')).toBe(8);

						FormService.setItemValue("Text.1", 3);
						expect(FormService.getItemValue('Text 7')).toBe(9);
						done();						
					},
					function() {
						//expect whatever not to be here
						expect(true).toBe(false);
						done();
					});
				_$httpBackend_.flush();						
			});
		});
	});
});